
###############################################################################
### Did 3G Make Afghan Insurgents Fight More Effectively? 
### Replication files for analyses
### Mehmet Erdem Arslan
###
### *Figure 1*: Evolution of Mobile Network Coverage in Afghanistan
###############################################################################

# setwd("your file path here")

library(tmap)
library(tmaptools)
library(sf)

# network coverage data (bi-yearly), afg_geo (country borders), afg_adm (admin2 borders, for naming major cities)
load("R/Figure1.RData")
# afg_geo: shape file, country borders 
# afg_adm: shape file, admin2 borders, used here for naming major cities 
# dat_union_..._...: polygons showing network coverage by 2G or 3G network AND year

tmap_options(check.and.fix = TRUE)

m09 <- tm_shape(afg_geo) + tm_borders() + tm_shape(dat_union_2g_3) + 
  tm_fill(col = "cadetblue3", alpha = 0.8) + 
  # tm_shape(dat_union_3g_3) + tm_fill(col = "red", alpha = 0.9) + 
  # commented out because does not contain any units 
  tm_layout(title="2009",  title.position = c("left", "top"), title.size = 0.75) + 
  tm_shape(afg_adm[afg_adm$OBJECTID %in% c(353, 46, 310, 269, 127, 110), ]) + 
  tm_text('DIST_34_NA', size = 0.4, alpha = 0.9) 

m11 <- tm_shape(afg_geo) + tm_borders() + tm_shape(dat_union_2g_11) + 
  tm_fill(col = "cadetblue3", alpha = 0.8) + 
  # tm_shape(dat_union_3g_11) + tm_fill(col = "red", alpha = 0.9) + 
  # commented out because does not contain any units 
  tm_layout(title="2011",  title.position = c("left", "top"), title.size = 0.75) + 
  tm_shape(afg_adm[afg_adm$OBJECTID %in% c(353, 46, 310, 269, 127, 110), ]) + 
  tm_text('DIST_34_NA', size = 0.4, alpha = 0.9) 

m13 <- tm_shape(afg_geo) + tm_borders() + tm_shape(dat_union_2g_19) + 
  tm_fill(col = "cadetblue3", alpha = 0.8) + 
  tm_shape(dat_union_3g_19) + tm_fill(col = "red", alpha = 0.9) + 
  tm_layout(title="2013",  title.position = c("left", "top"), title.size = 0.75) + 
  tm_shape(afg_adm[afg_adm$OBJECTID %in% c(353, 46, 310, 269, 127, 110), ]) + 
  tm_text('DIST_34_NA', size = 0.4, alpha = 0.9) 

m15 <- tm_shape(afg_geo) + tm_borders() + tm_shape(dat_union_2g_27) + 
  tm_fill(col = "cadetblue3", alpha = 0.8) + 
  tm_shape(dat_union_3g_27) + tm_fill(col = "red", alpha = 0.9) + 
  tm_layout(title="2015",  title.position = c("left", "top"), title.size = 0.75) + 
  tm_shape(afg_adm[afg_adm$OBJECTID %in% c(353, 46, 310, 269, 127, 110), ]) + 
  tm_text('DIST_34_NA', size = 0.4, alpha = 0.9) 

m17 <- tm_shape(afg_geo) + tm_borders() + tm_shape(dat_union_2g_35) + 
  tm_fill(col = "cadetblue3", alpha = 0.8) + 
  tm_shape(dat_union_3g_35) + tm_fill(col = "red", alpha = 0.9) + 
  tm_layout(title="2017",  title.position = c("left", "top"), title.size = 0.75) + 
  tm_shape(afg_adm[afg_adm$OBJECTID %in% c(353, 46, 310, 269, 127, 110), ]) + 
  tm_text('DIST_34_NA', size = 0.4, alpha = 0.9) 

m19 <- tm_shape(afg_geo) + tm_borders() + tm_shape(dat_union_2g_43) + 
  tm_fill(col = "cadetblue3", alpha = 0.8) + 
  tm_shape(dat_union_3g_43) + tm_fill(col = "red", alpha = 0.9) + 
  tm_layout(title="2019",  title.position = c("left", "top"), title.size = 0.75, legend.position = c('right', 'bottom')) + 
  tm_add_legend('fill', labels = c('2G\nnetwork', '3G\nnetwork'), col = c('cadetblue3', 'red'),
                is.portrait = FALSE, alpha = 0.8) +
  tm_shape(afg_adm[afg_adm$OBJECTID %in% c(353, 46, 310, 269, 127, 110), ]) + 
  tm_text('DIST_34_NA', size = 0.4, alpha = 0.9) 

map <- tmap_arrange(m09, m11, m13, m15, m17, m19, ncol = 2)

tmap_save(map, filename = "Outputs/Figure1.pdf", width = 17.5, height = 14.5, units = "cm")

rm(list=ls())




